<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <!-- 表单 搜索框 -->
      <el-form-item label="">
        <el-input
          clearable
          @clear="search()"
          v-model="searchMap.where.title"
          placeholder="题库名称"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search()">查询</el-button>
        <el-button
          type="primary"
          @click="
            form = {};
            dialogFormVisible = true;
            operate='添加题库';
          "
          >添加题库</el-button
        >
        <el-button type="danger" @click="deleteBatch()">批量删除</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表前的复选框 -->
    <el-table
      :data="repoList"
      style="width: 100%"
      @selection-change="selectItem"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <!-- 列表信息 -->
      <el-table-column prop="title" label="题库名称"> </el-table-column>
      <el-table-column prop="remark" label="题库备注"> </el-table-column>
      <el-table-column prop="radioCount" label="单选数量"> </el-table-column>
      <el-table-column prop="multiCount" label="多选数量"> </el-table-column>
      <el-table-column prop="radioCount" label="单选数量"> </el-table-column>
      <el-table-column prop="judgeCount" label="判断数量"> </el-table-column>
      <el-table-column prop="jdtCount" label="简答题数量"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit"
            @click="form = scope.row;dialogFormVisible = true;operate='修改题库';">修改</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br />
    <!-- 分页信息 -->
    <el-pagination
      style="float: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.now"
      :page-sizes="[5, 10, 50, 100]"
      :page-size="searchMap.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加题库 -->
    <el-dialog :title=operate :visible.sync="dialogFormVisible">
      <el-form ref="repo" :model="form" :rules="rules">
        <el-form-item label="题库名称" prop="title">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="题库描述">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save('repo')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { fetchData, save, deleteById, deleteBatch } from "@/api/repo";
export default {
  data() {
    //自定义规则
    var checkTitle = (rule, value, callback) => {
      if (value.indexOf("@") == -1) {
        return callback(new Error("标题必须包含@"));
      } else {
        callback();
      }
    };

    return {
      operate:"",
      deleteIds: [],
      dialogFormVisible: false,
      form: {},
      repoList: [],
      searchMap: {
        now: 1,
        size: 5,
        where: {
          ttle: "",
        },
      },
      total: 0,
      // 表单校验rules
      rules: {
        title: [
          { required: true, message: "请输入题库名称", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "长度在 5 到 20 个字符",
            trigger: "blur",
          },
          //自定义规则
          //{validator:checkTitle,trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    // 查询所有题库信息
    search() {
      fetchData(this.searchMap).then((res) => {
        console.log(res);
        this.repoList = res.list;
        this.total = res.total;
      });
    },
    // 切换每页条数
    handleSizeChange(size) {
      this.searchMap.size = size;
      this.search();
    },
    // 切换页数
    handleCurrentChange(page) {
      console.log(page);
      this.searchMap.now = page;
      this.search();
    },
    //添加
    save(formName) {
      //表单校验
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //保存
          save(this.form).then((res) => {
            this.$message({
              message: res.msg,
              type: res.code == 0 ? "success" : "danger",
            });
            this.dialogFormVisible = false;
            this.searchMap.now = 1;
            this.search();
          });
        } else {
          return false;
        }
      });
    },
    // 删除单个
    deleteById(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteById(id).then((res) => {
            this.$message({
              type: res.code == 0 ? "success" : "danger",
              message: res.msg,
            });
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //批删
    deleteBatch() {
      deleteBatch(this.deleteIds).then((res) => {
        this.$message({
          type: res.code == 0 ? "success" : "danger",
          message: res.msg,
        });
        this.search();
      });
    },
    //单选时获取选中的信息
    selectItem(selection) {
      console.log(selection);
      this.deleteIds = [];
      selection.forEach((item) => {
        this.deleteIds.push(item.id);
      });
    },
  },

  created() {
    this.search();
  },
};
</script>